module Menu where 

import Addressbook
import Data.Char

main (DB c g) = do menuLoop (DB c g)

dupa (DB c g)= do 
                putStrLn "--------------------------------------------------------------------------"
                putStrLn " a) dodaj \t  b) wyswietl c) koniec \t "
                putStrLn "--------------------------------------------------------------------------"
                n <- getLine
                case n of
                        "a" -> do  { 
                                
                                menuInsertContact (DB [] []);
                                }
                        "b" -> do  {  
                                menuShowContacts (DB c g);
                                dupa (DB c g);
                                }      
                        "c" -> do  { putStrLn "Wyjscie"
                                
                                }         

-- ---- GŁÓWNA PĘTLA PROGRAMU ----------------------------------
menuLoop (DB c g) = do putStrLn "=============================================================="
                       putStrLn "======= Ksiazka adresowa = Menu glowne ======================="
                       putStrLn "=============================================================="
                       putStrLn "1  Zarzadzanie kontaktami"
                       putStrLn "2  Zarzadzanie grupami"
                       putStrLn "3  Wyszukiwanie"
                       putStrLn "4  Kto ma dzisiaj urodziny"
                       putStrLn "0  Wyjscie"
                          
                       cmd <- getLine
                         
                       case cmd of "1" -> do manageContacts (DB c g)
                                   "2" -> do manageGroups   (DB c g)
                                   "3" -> do find           (DB c g)
                                   "4" -> do whereIsParty   (DB c g)
                                   "0" -> do putStrLn       "Koniec."
                                   otherwise -> do putStrLn "Nieprawidlowy nr opcji"
                                                   menuLoop (DB c g)

-- ---- ZARZADZANIE KONTAKTAMI ---------------------------------
manageContacts (DB c g) = do putStrLn "=============================================================="
                             putStrLn "======= Ksiazka adresowa = Zarzadzanie kontaktami ============"
                             putStrLn "=============================================================="
                             putStrLn "1  Wyswietl kontakty"
                             putStrLn "2  Dodaj kontakt"
                             putStrLn "3  Edytuj kontakt"
                             putStrLn "4  Usun kontakt"
                             putStrLn "0  Wroc"
         
                             cmd <- getLine
         
                             case cmd of "1" -> do menuShowContacts     (DB c g)
                                         "2" -> do menuInsertContact    (DB c g)
                                         "3" -> do menuUpdateContact    (DB c g) 
                                         "4" -> do menuDeleteContact    (DB c g)
                                         "0" -> do menuLoop             (DB c g)
                                         otherwise -> do putStrLn       "Nieprawidlowy nr opcji"
                                                         manageContacts (DB c g)

-- ---- ZARZADZANIE GRUPAMI ------------------------------------
manageGroups (DB c g) = do putStrLn "=============================================================="
                           putStrLn "======= Ksiazka adresowa = Zarzadzanie grupami ==============="
                           putStrLn "=============================================================="
                           putStrLn "1  Wyswietl grupy"
                           putStrLn "2  Dodaj grupe"
                           putStrLn "3  Edytuj nazwe grupy"
                           putStrLn "4  Scal grupy"
                           putStrLn "5  Zarzadzaj czlonkami grupy"
                           putStrLn "6  Usuń grupe"
                           putStrLn "0  Wroc"
         
                           cmd <- getLine
         
                           case cmd of "1" -> do menuShowGroups     (DB c g)
                                       "2" -> do menuAddGroups      (DB c g)
                                       "3" -> do menuUpdateGroups   (DB c g)
                                       "4" -> do menuSumGroups      (DB c g)
                                       "5" -> do menuManageMembers  (DB c g)
                                       "6" -> do menuDeleteGroups   (DB c g)
                                       "0" -> do menuLoop           (DB c g)
                                       otherwise -> do putStrLn     "Nieprawidlowy nr opcji"
                                                       manageGroups (DB c g)
        
-- ---- SZUKAJKI -----------------------------------------------
find (DB c g) = do putStrLn "=============================================================="
                   putStrLn "======= Ksiazka adresowa = Szukajki =========================="
                   putStrLn "=============================================================="
                   putStrLn "1  Znajdź kontakt po ..."
                   putStrLn "2  Znajdź czlonkow grupy"
                   putStrLn "0  Wroc"
         
                   cmd <- getLine
         
                   case cmd of "1" -> do findContact          (DB c g)
                               "2" -> do findMembers          (DB c g)
                               "0" -> do menuLoop             (DB c g)
                               otherwise -> do putStrLn       "Nieprawidlowy nr opcji"
                                               manageContacts (DB c g)

-- KONTAKTY

menuShowContacts (DB c g) = do putStrLn ( contactsToString c )
                             --  manageContacts (DB c g)
 
menuInsertContact (DB c g) = do putStrLn "====================================="
                                putStr "Podaj imie: "
                                name <- getLine
                                putStr "Podaj nazwisko: "
                                surname <- getLine
                                putStr "Podaj firmę: "
                                company <- getLine
                                phone <- loadPhoneNumber                                
                                mail <- loadMail                           
                                birthday <- loadData
                                putStrLn "bangla"
                  
loadPhoneNumber =    do putStr "Podaj numer telefonu: "
                        phone <- getLine 
                        if (isNumberFromString phone) 
                        then return phone
                        else do
                             putStr "Podany został niepoprawny numer telefonu. Numer telefonu może być złożony tylko z liczb. "
                             loadPhoneNumber
                             
loadData =          do  putStr "Podaj datę urodzenia w foramcie dd-mm-rrrr: "
                        date <- getLine
                        if ((numberOfDashes date)==2) 
                        then  let items = (split "-" date) in
                              if ((isDayCorrect (items!!0))&&(isMonthCorrect (items!!1))&&(isYearCorrect (items!!2)))
                              then return date
                              else do
                                   putStr "Podana została nieprawidłowa data. "
                                   loadData 
                        else do
                             putStr "Podana została nieprawidłowa data. Kolejne liczby należy oddzielić myślnikami. "
                             loadData
                             
loadMail=       do      putStr "Podaj adres e-mail: "
                        mail <- getLine
                        if ((numberOfAt mail)==1)
                        then return mail
                        else do
                             putStr "Podany został nieprawidłowy adres e-mail. "
                             loadMail 
                             

                                                                
                                

menuUpdateContact (DB c g) = do putStrLn "====================================="
                                putStr "Podaj id kontaktu: "
                                -- id <- getLine
                                 
menuDeleteContact (DB c g) = do putStrLn "====================================="
                                putStr "Podaj id kontaktu: "
                                id <- getLine
                                let id = 0
                                manageContacts ( DB ( deleteContact c id ) g )        

-- GRUPY
                                 
menuShowGroups (DB c g) = do putStrLn "====================================="
                             manageGroups ( DB c g )  

menuAddGroups (DB c g) = do putStrLn "====================================="
                            manageGroups ( DB c g )
                             
menuUpdateGroups (DB c g) = do putStrLn "====================================="
                               manageGroups ( DB c g )
                             
menuSumGroups (DB c g) = do putStrLn "====================================="
                            manageGroups ( DB c g )
                             
menuManageMembers (DB c g) = do putStrLn "====================================="
                                manageGroups ( DB c g )
                             
menuDeleteGroups (DB c g) = do putStrLn "====================================="
                               manageGroups ( DB c g )

-- SZUKAJKI
                             
findContact (DB c g) = do putStrLn "====================================="
                          find ( DB c g )
                             
findMembers (DB c g) = do putStrLn "====================================="
                          find ( DB c g )
                             
-- KTO MA DZISIAJ URODZINY

whereIsParty (DB c g) = error "w DUPENCJE!"

-- FUNKCJE POMOCNICZE
isNumberFromString [] = False
isNumberFromString [x] = if isDigit x then True else False
isNumberFromString (x:xs) = if isDigit x then isNumberFromString xs else False

numberOfDashes [] =0
numberOfDashes [x]=if ([x] =="-") then 1 else 0
numberOfDashes (x:xs)= if ([x] =="-") then 1+(numberOfDashes xs) else numberOfDashes xs

numberOfAt [] =0
numberOfAt [x]=if ([x] =="@") then 1 else 0
numberOfAt (x:xs)= if ([x] =="@") then 1+(numberOfAt xs) else numberOfAt xs
                                                         
string2int [] = 0
string2int [x] | isDigit x == True = digitToInt x
               | otherwise = 0
string2int (x:xs) | isDigit x == True = 10^length xs * digitToInt x + string2int xs
                  | otherwise = 0
                                                        
isDayCorrect day = if ((string2int day) > 0) && ((string2int day) < 32) then True else False


isMonthCorrect month = if ((string2int month) > 0) && ((string2int month) < 13) then True else False
                       

isYearCorrect year = if ((string2int year) > 1800) && ((string2int year) < 2013) then True else False
                        
                                                 